/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entidades;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;


@Entity
@Table(name = "VENTA")
@NamedQueries({
    @NamedQuery(name = "Venta.findAll", query = "SELECT v FROM Venta v"),
    @NamedQuery(name = "Venta.findByCodigo", query = "SELECT v FROM Venta v WHERE v.codigo = :codigo"),
    @NamedQuery(name = "Venta.findByFecha", query = "SELECT v FROM Venta v WHERE v.fecha = :fecha"),
    @NamedQuery(name = "Venta.findByImporte", query = "SELECT v FROM Venta v WHERE v.importe = :importe"),
    @NamedQuery(name = "Venta.findByCantidad", query = "SELECT v FROM Venta v WHERE v.cantidad = :cantidad"),
    @NamedQuery(name = "Venta.findByEstado", query = "SELECT v FROM Venta v WHERE v.estado = :estado"),
    @NamedQuery(name = "Venta.findByComision", query = "SELECT v FROM Venta v WHERE v.comision = :comision"),

    @NamedQuery(name = "Venta.findTotalVentasMes", query = "SELECT sum(v.importe) FROM Venta v WHERE MONTH(v.fecha) = :mes AND YEAR(v.fecha) = :anio GROUP BY YEAR(v.fecha), MONTH(v.fecha)"),
    @NamedQuery(name = "Venta.findTotalVentasAnio", query = "SELECT sum(v.importe) FROM Venta v WHERE YEAR(v.fecha) = :anio GROUP BY YEAR(v.fecha)"),
    @NamedQuery(name = "Venta.findTotalComisionesMes", query = "SELECT sum(v.comision) FROM Venta v WHERE MONTH(v.fecha) = :mes AND YEAR(v.fecha) = :anio GROUP BY YEAR(v.fecha), MONTH(v.fecha)"),
    @NamedQuery(name = "Venta.findTotalComisionesAnio", query = "SELECT sum(v.comision) FROM Venta v WHERE YEAR(v.fecha) = :anio GROUP BY YEAR(v.fecha)"),

    @NamedQuery(name = "Venta.findIngresosMes", query = "SELECT MONTH(v.fecha), sum(v.importe) FROM Venta v WHERE YEAR(v.fecha) = :anio GROUP BY YEAR(v.fecha), MONTH(v.fecha)")
})

public class Venta implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "CODIGO")
    private Integer codigo;
    @Column(name = "FECHA")
    @Temporal(TemporalType.DATE)
    private Date fecha;
    @Column(name = "IMPORTE")
    private Double importe;
    @Column(name = "CANTIDAD")
    private Integer cantidad;
    @Column(name = "ESTADO")
    private String estado;
    @Column(name = "COMISION")
    private Double comision;
   
    @JoinColumn(name = "ARTICULO", referencedColumnName = "CODIGO")
    @ManyToOne(optional = false)
    private Articulo articulo;
    @JoinColumn(name = "CLIENTE", referencedColumnName = "CODIGO")
    @ManyToOne(optional = false)
    private Cliente cliente;
    
    @JoinColumn(name = "VENDEDOR", referencedColumnName = "CODIGO")
    @ManyToOne(optional = false)
    private Vendedor vendedor;

    public Venta() {
    }

    public Venta(Integer codigo) {
        this.codigo = codigo;
    }

    public Integer getCodigo() {
        return codigo;
    }

    public void setCodigo(Integer codigo) {
        Integer oldCodigo = this.codigo;
        this.codigo = codigo;
        changeSupport.firePropertyChange("codigo", oldCodigo, codigo);
    }

    public Date getFecha() {
        return fecha;
    }

    public void setFecha(Date fecha) {
        System.out.println("setting date to object Venta..");
        this.fecha = fecha;
    }

    public Double getImporte() {
        return importe;
    }

    public void setImporte(Double importe) {
        Double oldImporte = this.importe;
        this.importe = importe;
        changeSupport.firePropertyChange("importe", oldImporte, importe);
    }

    public Integer getCantidad() {
        return cantidad;
    }

    public void setCantidad(Integer cantidad) {
        Integer oldCantidad = this.cantidad;
        this.cantidad = cantidad;
        changeSupport.firePropertyChange("cantidad", oldCantidad, cantidad);
    }

    public String getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        String oldEstado = this.estado;
        this.estado = estado;
        changeSupport.firePropertyChange("estado", oldEstado, estado);
    }

    public Articulo getArticulo() {
        return articulo;
    }

    public void setArticulo(Articulo articulo) {
        Articulo oldArticulo = this.articulo;
        this.articulo = articulo;
        changeSupport.firePropertyChange("articulo", oldArticulo, articulo);
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        Cliente oldCliente = this.cliente;
        this.cliente = cliente;
        changeSupport.firePropertyChange("cliente", oldCliente, cliente);
    }

    public Double getComision() {
        return comision;
    }

    public void setComision(Double comision) {
        Double oldComision = this.comision;
        this.comision = comision;
        changeSupport.firePropertyChange("comision", oldComision, comision);
    }

    public Vendedor getVendedor() {
        return vendedor;
    }

    public void setVendedor(Vendedor vendedor) {
        Vendedor oldVendedor = this.vendedor;
        this.vendedor = vendedor;
        changeSupport.firePropertyChange("vendedor", oldVendedor, vendedor);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (codigo != null ? codigo.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Venta)) {
            return false;
        }
        Venta other = (Venta) object;
        if ((this.codigo == null && other.codigo != null) || (this.codigo != null && !this.codigo.equals(other.codigo))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entidades.Venta[codigo=" + codigo + "]";
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        changeSupport.removePropertyChangeListener(listener);
    }

}
